package com.sita.linboard.person;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sita.linboard.R;
import com.sita.linboard.driverInfo.ClearBackBean;
import com.sita.linboard.http.RestClient;
import com.sita.linboard.journey.OrderRequest;
import com.sita.linboard.utils.LogUtils;
import com.sita.linboard.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Jason on 2016/9/19.
 */
public class DriveTripAdapter extends RecyclerView.Adapter<DriveTripAdapter.MyHolder> {
    private Context context;
    private DriverTripBackBean data;

    public DriveTripAdapter(Context context) {
        this.context = context;

        data = new DriverTripBackBean();
    }

    public void setData(DriverTripBackBean driverTripBackBeen,ProgressBar bar){
        data = driverTripBackBeen;
        notifyDataSetChanged();
        if (bar!=null){
            bar.setVisibility(View.GONE);
        }
    }
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_drivertrip,parent,false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        if (data!=null&&data.getErrorCode().equals("0")){
            holder.tripTime.setText(data.getData().get(position).getDeparture_time());
            if (data.getData().get(position).getState()==4){
                holder.tripUrged.setVisibility(View.VISIBLE);
                holder.tripPay.setText("未支付");
            }
            else {
                holder.tripUrged.setVisibility(View.GONE);
                holder.tripPay.setText("已支付");
            }
            holder.tripStart.setText(context.getString(R.string.start_location,data.getData().get(position).getDeparture().getAddress()));
            holder.tripEnd.setText(context.getString(R.string.end_location,data.getData().get(position).getArrival().getAddress()));
            holder.tripMoney.setText("¥"+data.getData().get(position).getPayment());
            holder.position = position;
        }
    }

    @Override
    public int getItemCount() {
        if (data.getData()==null){
            return 0;
        }
        else {
          return data.getData().size();
        }

    }

    class MyHolder extends RecyclerView.ViewHolder{
        TextView  tripTime,tripMoney,
        tripStart,tripEnd,tripPay;
        LinearLayout tripUrged;
        int position;
        public MyHolder(View itemView) {
            super(itemView);
            tripTime = (TextView) itemView.findViewById(R.id.trip_time);
            tripMoney = (TextView) itemView.findViewById(R.id.trip_money);
            tripStart = (TextView) itemView.findViewById(R.id.trip_start);
            tripEnd = (TextView) itemView.findViewById(R.id.trip_end);
            tripPay = (TextView) itemView.findViewById(R.id.trip_paystate);
            tripUrged = (LinearLayout) itemView.findViewById(R.id.trip_urged);
            tripUrged.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    OrderRequest request = new OrderRequest();
                    request.trip_id = data.getData().get(position).getTrip_id();
                    RestClient.getRestService().urgedOrder(request, new Callback<ClearBackBean>() {
                        @Override
                        public void success(ClearBackBean clearBackBean, Response response) {
                            if (clearBackBean.getErrorCode().equals("0")&&clearBackBean.getData()==null){
                                LogUtils.d("催单成功");
                                ToastUtils.show("已向乘客发送催单请求",5);

                            }
                        }

                        @Override
                        public void failure(RetrofitError error) {
                            LogUtils.d("催单失败");
                        }
                    });

                }
            });
        }
    }
}
